package com.imstlife.vending.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.imstlife.vending.R;

public class GvBaseAdapter extends BaseAdapter {
    private Context context;
    private int count;
    private GVInter gvInter;
    public interface GVInter{
        void BtnOnClick(int num);
    }
    public GvBaseAdapter(Context context, int count,GVInter gvInter) {
        this.context = context;
        this.count = count;
        this.gvInter = gvInter;
    }

    @Override
    public int getCount() {
        return count;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        //如果view未被实例化过，缓存池中没有对应的缓存
        if (convertView == null) {
            viewHolder = new ViewHolder();
            // 由于我们只需要将XML转化为View，并不涉及到具体的布局，所以第二个参数通常设置为null
            convertView = View.inflate(context,R.layout.gv_item, null);
            //对viewHolder的属性进行赋值
            viewHolder.num = (Button) convertView.findViewById(R.id.gv_itembtn);
            //通过setTag将convertView与viewHolder关联
            convertView.setTag(viewHolder);
        }else{//如果缓存池中有对应的view缓存，则直接通过getTag取出viewHolder
            viewHolder = (ViewHolder) convertView.getTag();
        }
        // 设置控件的数据
        viewHolder.num.setText("货道"+(position+1));
        viewHolder.num.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                gvInter.BtnOnClick(position+1);
            }
        });
        return convertView;
    }
    class ViewHolder{
        public Button num;
    }
}
